package com.behome.stream.framework.config;

import com.behome.stream.framework.Inteceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WebConfig
 * @Description 配置http访问本地静态路径文件
 * @Author Ljy
 * @Date 2021/3/1 15:58
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${folder.path}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//          访问路径：http://localhost:8080/upload/xxxxx
//          "/upload/**" 为前端URL访问路径
//          "file:" + uploadPath 是本地磁盘映射
        registry.addResourceHandler("/record/**").addResourceLocations("file:" + uploadPath + File.separator);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 放行路径
        List<String> patterns = new ArrayList<>();
        patterns.add("/record/**");
        registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/**").excludePathPatterns(patterns);
    }
}
